package com.navi.mould1.reportUtils;

import com.google.gson.Gson;
import com.navi.bean.in.ConditionBean;
import org.apache.commons.beanutils.BeanUtils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class reportUtil {

    /*** map字符串转Map，然后将字段映射到ConditionBean实体类中 **/
    public static ConditionBean getCondition(String hashMapString) {
        Map<String, Object> requestMap = new Gson().fromJson(hashMapString,  new HashMap<String, Object>().getClass());
        ConditionBean conditions = null;
        try{
            conditions = ConditionBean.class.newInstance();
            BeanUtils.populate(conditions, requestMap);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return conditions;
    }


    /*** 判断字符串是不是以数字开头 **/
    public static boolean isStartWithNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str.charAt(0) + "");
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }


    /*** Timestamp转YYYY-MM-dd格式 **/
    public static String timeStamp2YMD(Timestamp timestamp){
        return new SimpleDateFormat("yyyy-MM-dd").format(timestamp.getTime());
    }


    /*** 根据8:00 - 8:00的规则，确定timestamp属于哪一天 **/
    public static String getCurrenDay(Timestamp timestamp) {
        if (timestamp == null) return null;
        Timestamp timestamp_eight = Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd 08:00:00").format(timestamp.getTime()));
        return timestamp.getTime() < timestamp_eight.getTime() ? timeStamp2YMD(new Timestamp(timestamp.getTime() - 24*60*60*1000)): timeStamp2YMD(timestamp);  //当前大于小于8:00属于哪一天
    }

    /*** 根据8:00 - 8:00的规则，确定timestamp属于哪一天 **/
    public static String getCurrenDay(String date, String hour) {
        Timestamp timestamp = Timestamp.valueOf(date + " " + hour + ":00:00");
        if (timestamp == null) return null;
        Timestamp timestamp_eight = Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd 08:00:00").format(timestamp.getTime()));
        return timestamp.getTime() < timestamp_eight.getTime() ? timeStamp2YMD(new Timestamp(timestamp.getTime() - 24*60*60*1000)): timeStamp2YMD(timestamp);  //当前大于小于8:00属于哪一天
    }


    /*** 根据8:00 - 20:00的规则，确定timestamp属于哪一天 **/
    public static String getCurrenDay2Shift(Timestamp timestamp) {
        if (timestamp == null) return null;
        Timestamp timestamp_day_eight = Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd 08:00:00").format(timestamp.getTime()));
        final String currentDay = timestamp.getTime() < timestamp_day_eight.getTime() ? timeStamp2YMD(new Timestamp(timestamp.getTime() - 24*60*60*1000)) : timeStamp2YMD(timestamp);  //当前大于小于8:00属于哪一天

        Timestamp timestamp_night_eight = Timestamp.valueOf(new SimpleDateFormat(currentDay + " 20:00:00").format(timestamp.getTime()));
        final String currentShift = timestamp.getTime() < timestamp_night_eight.getTime() ? "D" : "N";

        return currentDay + "-" + currentShift;
    }

    /*** 根据8:00 - 20:00的规则，确定timestamp属于哪一天 **/
    public static String getCurrenDay2Shift(String date, String hour) {
        Timestamp timestamp = Timestamp.valueOf(date + " " + hour + ":00:00");
        if (timestamp == null) return null;
        Timestamp timestamp_day_eight = Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd 08:00:00").format(timestamp.getTime()));
        final String currentDay = timestamp.getTime() < timestamp_day_eight.getTime() ? timeStamp2YMD(new Timestamp(timestamp.getTime() - 24*60*60*1000)) : timeStamp2YMD(timestamp);  //当前大于小于8:00属于哪一天

        Timestamp timestamp_night_eight = Timestamp.valueOf(new SimpleDateFormat(currentDay + " 20:00:00").format(timestamp.getTime()));
        final String currentShift = timestamp.getTime() < timestamp_night_eight.getTime() ? "D" : "N";

        return currentDay + "-" + currentShift;
    }


    /*** 排序方法(解决中文、字母、数字的字符串排序) **/
    public static int compareTo3(String str1, String str2) {
        if(null == str1){
            if(null == str2){
                return 0;
            } else {
                return -1;
            }
        } else if(null == str2){
            return 1;
        }

        if(str1.equals(str2)){
            return 0;
        }

        // 中间 1-多个数字
        Pattern pattern = Pattern.compile("\\D*(\\d+)\\D*");
        Matcher matcher1 = pattern.matcher(str1);
        Matcher matcher2 = pattern.matcher(str2);

        int index1_step = 0;
        int index2_step = 0;
        while(matcher1.find()){
            String s1 = matcher1.group(1);
            String s2 = null;
            if(matcher2.find()){
                s2 = matcher2.group(1);
            }
            int index1 = str1.indexOf(s1, index1_step);
            int index2 = str2.indexOf(s2, index2_step);
            //
            index1_step = index1;
            index2_step = index2;
            // 索引相等的情况下
            if(index1 == index2){
                String pre1 = str1.substring(0, index1);
                String pre2 = str2.substring(0, index2);
                if(pre1.equals(pre2)){
                    //
                    long num1 = Long.parseLong(s1);
                    long num2 = Long.parseLong(s2);
                    //
                    if(num1 == num2){
                        // 比较下一组
                        continue;
                    } else {
                        return (int)(num1 - num2);
                    }
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        return str1.compareTo(str2);
    }


    /*** 排序方法(解决中文、字母、数字的字符串排序) **/
    static class Int{
        public int i;
    }
    public static int findDigitEnd(char[] arrChar, Int at) {
        int k = at.i;
        char c = arrChar[k];
        boolean bFirstZero = (c == '0');
        while (k < arrChar.length) {
            c = arrChar[k];
            //first non-digit which is a high chance.
            if (c > '9' || c < '0') {
                break;
            }
            else if (bFirstZero && c == '0') {
                at.i++;
            }
            k++;
        }
        return k;
    }
    public static int compareTo2(String str1, String str2) {
        char[] a = str1.toCharArray();
        char[] b = str2.toCharArray();

        if(a != null || b != null){
            Int aNonzeroIndex = new Int();
            Int bNonzeroIndex = new Int();
            int aIndex = 0, bIndex = 0,
                    aComparedUnitTailIndex, bComparedUnitTailIndex;

            //Pattern pattern = Pattern.compile("D*(d+)D*");
            //Matcher matcher1 = pattern.matcher(a);
            //Matcher matcher2 = pattern.matcher(b);
            //if(matcher1.find() && matcher2.find()) {
            //    String s1 = matcher1.group(1);
            //    String s2 = matcher2.group(1);
            //}

            while(aIndex < a.length && bIndex < b.length){
                //aIndex <
                aNonzeroIndex.i = aIndex;
                bNonzeroIndex.i = bIndex;
                aComparedUnitTailIndex = findDigitEnd(a, aNonzeroIndex);
                bComparedUnitTailIndex = findDigitEnd(b, bNonzeroIndex);
                //compare by number
                if (aComparedUnitTailIndex > aIndex && bComparedUnitTailIndex > bIndex)
                {
                    int aDigitIndex = aNonzeroIndex.i;
                    int bDigitIndex = bNonzeroIndex.i;
                    int aDigit = aComparedUnitTailIndex - aDigitIndex;
                    int bDigit = bComparedUnitTailIndex - bDigitIndex;
                    //compare by digit
                    if(aDigit != bDigit)
                        return aDigit - bDigit;
                    //the number of their digit is same.
                    while (aDigitIndex < aComparedUnitTailIndex){
                        if (a[aDigitIndex] != b[bDigitIndex])
                            return a[aDigitIndex] - b[bDigitIndex];
                        aDigitIndex++;
                        bDigitIndex++;
                    }
                    //if they are equal compared by number, compare the number of '0' when start with "0"
                    //ps note: paNonZero and pbNonZero can be added the above loop "while", but it is changed meanwhile.
                    //so, the following comparsion is ok.
                    aDigit = aNonzeroIndex.i - aIndex;
                    bDigit = bNonzeroIndex.i - bIndex;
                    if (aDigit != bDigit)
                        return aDigit - bDigit;
                    aIndex = aComparedUnitTailIndex;
                    bIndex = bComparedUnitTailIndex;
                }else{
                    if (a[aIndex] != b[bIndex])
                        return a[aIndex] - b[bIndex];
                    aIndex++;
                    bIndex++;
                }

            }

        }
        return a.length - b.length;
    }













}
